package com.neuronbit.businessflow.runtime;

import java.util.function.*;

/**
 * The type Null safe.
 */
public final class NullSafe {
    /**
     * Char, String, Object, BigDecimal, etc.
     *
     * @param <T>      the type parameter
     * @param supplier the supplier
     * @return t t
     */
    public static <T> T get(Supplier<T> supplier) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return null;
        }
    }

    /**
     * Get boolean.
     *
     * @param supplier the supplier
     * @return the boolean
     */
    public static boolean get(BooleanSupplier supplier) {
        try {
            return supplier.getAsBoolean();
        } catch (NullPointerException e) {
            return false;
        }
    }

    /**
     * Gets byte.
     *
     * @param supplier the supplier
     * @return the byte
     */
    public static byte getByte(Supplier<Byte> supplier) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Gets short.
     *
     * @param supplier the supplier
     * @return the short
     */
    public static short getShort(Supplier<Short> supplier) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Get int.
     *
     * @param supplier the supplier
     * @return the int
     */
    public static int get(IntSupplier supplier) {
        try {
            return supplier.getAsInt();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Get long.
     *
     * @param supplier the supplier
     * @return the long
     */
    public static long get(LongSupplier supplier) {
        try {
            return supplier.getAsLong();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Gets float.
     *
     * @param supplier the supplier
     * @return the float
     */
    public static float getFloat(Supplier<Float> supplier) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Get double.
     *
     * @param supplier the supplier
     * @return the double
     */
    public static double get(DoubleSupplier supplier) {
        try {
            return supplier.getAsDouble();
        } catch (NullPointerException e) {
            return 0;
        }
    }

    /**
     * Char, String, Object, BigDecimal, etc.
     *
     * @param <T>          type
     * @param supplier     value supplier
     * @param defaultValue default value if NPE throw
     * @return value or default value
     */
    public static <T> T get(Supplier<T> supplier, T defaultValue) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Get boolean.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the boolean
     */
    public static boolean get(BooleanSupplier supplier, boolean defaultValue) {
        try {
            return supplier.getAsBoolean();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Gets byte.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the byte
     */
    public static byte getByte(Supplier<Byte> supplier, byte defaultValue) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Gets short.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the short
     */
    public static short getShort(Supplier<Short> supplier, short defaultValue) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Get int.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the int
     */
    public static int get(IntSupplier supplier, int defaultValue) {
        try {
            return supplier.getAsInt();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Get long.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the long
     */
    public static long get(LongSupplier supplier, long defaultValue) {
        try {
            return supplier.getAsLong();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Gets float.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the float
     */
    public static float getFloat(Supplier<Float> supplier, float defaultValue) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }

    /**
     * Get double.
     *
     * @param supplier     the supplier
     * @param defaultValue the default value
     * @return the double
     */
    public static double get(DoubleSupplier supplier, double defaultValue) {
        try {
            return supplier.getAsDouble();
        } catch (NullPointerException e) {
            return defaultValue;
        }
    }
}
